		
#include "LoadingState.h"

LoadingState::LoadingState()
{

	mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
		("SdkTrays/ProgressBar", "BorderPanel", name);
	mElement->setWidth(width);
	Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)mElement;
	mTextArea = (Ogre::TextAreaOverlayElement*)c->getChild(getName() + "/ProgressCaption");
	Ogre::OverlayContainer* commentBox = (Ogre::OverlayContainer*)c->getChild(getName() + "/ProgressCommentBox");
	commentBox->setWidth(commentBoxWidth);
	commentBox->setLeft(-(commentBoxWidth + 5));
	mCommentTextArea = (Ogre::TextAreaOverlayElement*)commentBox->getChild(commentBox->getName() + "/ProgressCommentText");
	mMeter = c->getChild(getName() + "/ProgressMeter");
	mMeter->setWidth(width - 10);
	mFill = ((Ogre::OverlayContainer*)mMeter)->getChild(mMeter->getName() + "/ProgressFill");
	setCaption(caption);
}

LoadingState::~LoadingState()
{


}

/*-----------------------------------------------------------------------------
| Sets the progress as a percentage.
-----------------------------------------------------------------------------*/
void setProgress(Ogre::Real progress)
{
	mProgress = Ogre::Math::Clamp<Ogre::Real>(progress, 0, 1);
	mFill->setWidth(std::max<int>((int)mFill->getHeight(), (int)(mProgress * (mMeter->getWidth() - 2 * mFill->getLeft()))));
}

/*-----------------------------------------------------------------------------
| Shows loading bar. Also takes job settings: the number of resource groups
| to be initialised, the number of resource groups to be loaded, and the
| proportion of the job that will be taken up by initialisation. Usually,
| script parsing takes up most time, so the default value is 0.7.
-----------------------------------------------------------------------------*/
void showLoadingBar(unsigned int numGroupsInit = 1, unsigned int numGroupsLoad = 1,
	Ogre::Real initProportion = 0.7)
{
	if (mDialog) closeDialog();
	if (mLoadBar) hideLoadingBar();

	mLoadBar = new ProgressBar(mName + "/LoadingBar", "Loading...", 400, 308);
	Ogre::OverlayElement* e = mLoadBar->getOverlayElement();
	mDialogShade->addChild(e);
	e->setVerticalAlignment(Ogre::GVA_CENTER);
	e->setLeft(-(e->getWidth() / 2));
	e->setTop(-(e->getHeight() / 2));

	Ogre::ResourceGroupManager::getSingleton().addResourceGroupListener(this);
	mCursorWasVisible = isCursorVisible();
	hideCursor();
	mDialogShade->show();

	// calculate the proportion of job required to init/load one group

	if (numGroupsInit == 0 && numGroupsLoad != 0)
	{
		mGroupInitProportion = 0;
		mGroupLoadProportion = 1;
	}
	else if (numGroupsLoad == 0 && numGroupsInit != 0)
	{
		mGroupLoadProportion = 0;
		if (numGroupsInit != 0) mGroupInitProportion = 1;
	}
	else if (numGroupsInit == 0 && numGroupsLoad == 0)
	{
		mGroupInitProportion = 0;
		mGroupLoadProportion = 0;
	}
	else
	{
		mGroupInitProportion = initProportion / numGroupsInit;
		mGroupLoadProportion = (1 - initProportion) / numGroupsLoad;
	}
}

		void hideLoadingBar()
		{
			if (mLoadBar)
			{
				mLoadBar->cleanup();
				delete mLoadBar;
				mLoadBar = 0;

				Ogre::ResourceGroupManager::getSingleton().removeResourceGroupListener(this);
				if (mCursorWasVisible) showCursor();
				mDialogShade->hide();
			}
		}

bool isLoadingBarVisible()
{
	return mLoadBar != 0;
}

void scriptParseEnded(const Ogre::String& scriptName, bool skipped)
{
	mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc);
	mWindow->update();
}

void resourceLoadStarted(const Ogre::ResourcePtr& resource)
{
	mLoadBar->setComment(resource->getName());
	mWindow->update();
}

void resourceLoadEnded()
{
	mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc);
	mWindow->update();
}